package Json_demo;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class Json_read_ListHashmap {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
// khoi tao thu vien objmapper
		ObjectMapper objma = new ObjectMapper();
//bat cua so chon file
		JFileChooser objFile = new JFileChooser();
        objFile.setCurrentDirectory(new java.io.File("."));
        objFile.setDialogTitle("Chọn file: ");
        objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        String[] EXTENSION = new String[] { "json" };
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JSON files", EXTENSION);
        objFile.setFileFilter(filter);
        objFile.setMultiSelectionEnabled(false);

        int returnVal = objFile.showOpenDialog(null);
        String ParentPath = "";
        String FileName = "";
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            // ParentPath = objFile.getSelectedFile().getParent();
         FileName = objFile.getSelectedFile().getPath();
        
 // khoi tao 1 List Hashmap de don du lieu
         try {
			List<HashMap<Integer, Teacher>> HashmapList = objma.readValue(new File(FileName), new TypeReference<List<HashMap<Integer,Teacher>>>() {
			});
// khoi tao 2 hashmap don du lieu
			HashMap<Integer, Teacher> Hasmap01 = HashmapList.get(0);
			HashMap<Integer, Teacher> Hasmap02 = HashmapList.get(1);
			System.out.println("--------Giao vien toan------");
			for(int i = 0; i < Hasmap01.size();i++){
				System.out.println("RollNo :"+Hasmap01.get(i).getRollNo());
				System.out.println("FullName :"+Hasmap01.get(i).getFullName());
				System.out.println("Mon day :"+Hasmap01.get(i).getMon());
				System.out.println("Diem so :"+Hasmap01.get(i).getDiemSo());
			}
			System.out.println("--------Giao vien van-------");
			for(int i = 0;i<Hasmap02.size();i++){
				System.out.println("RollNo :"+Hasmap02.get(i).getRollNo());
				System.out.println("FullName :"+Hasmap02.get(i).getFullName());
				System.out.println("Mon day :"+Hasmap02.get(i).getMon());
				System.out.println("Diem so :"+Hasmap02.get(i).getDiemSo());
			}
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        }
	}

}
